package com.zhongan.user.controller;

import com.zhongan.common.model.ControllerTemplate;
import com.zhongan.common.web.SpringBeanProxy;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.util.Map;

/**
 * 模板服务
 *
 * @author za-shaojian
 */
@Api(value = "模板controller", tags = {"模板操作接口"})
@RestController
@RequestMapping("/template")
public class TemplateController {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private VelocityEngine velocityEngine;

    @ApiOperation(value = "service生成controller服务", notes = "根据service生成controller", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query" ,name = "serviceCode", value = "微服务服务代码", required = false, dataType = "String"),
            @ApiImplicitParam(paramType="query",name = "serviceClass", value = "对外接口的服务类名", required = false, dataType = "String")
    })
    @RequestMapping(value = "/serviceToController", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String serviceToController(@RequestParam(value = "serviceCode", required = true) String serviceCode, @RequestParam(value = "serviceClass", required = true) String serviceClass, HttpServletRequest request) {
        //得到所有的待转换的service
        Map<String, ControllerTemplate> functionCodeCatalogMap = SpringBeanProxy.getFunctionCodeCatalogMap();
        for (Map.Entry<String, ControllerTemplate> entry : functionCodeCatalogMap.entrySet()) {
            Map<String, String> functionCodeListMap = SpringBeanProxy.getFunctionListByCatalog(entry.getKey());
            ControllerTemplate controllerTemplate = entry.getValue();
            VelocityContext context = new VelocityContext();
            context.put("controllerTemplate", controllerTemplate);
            Template template = Velocity.getTemplate("templates/controller.vm");
            PrintWriter writer = null;
            try {
                writer = new PrintWriter("D:\\controller\\DemoController.java", "UTF-8");
                template.merge(context, writer);
                System.out.println(writer.toString());
                writer.flush();
                writer.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}